home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / intrvews / xgrab.lha / xgrab / ui / ipaint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-24  |  4.7 KB  |  228 lines

  1. /**
  2.    GRAB Graph Layout and Browser System
  3.  
  4.    Copyright (c) 1987, 1988, 1989 Stanford University
  5.    Copyright (c) 1989, Tera Computer Company
  6.  **/
  7.  
  8.   /**
  9.      Stolen from the InterViews 2.5 bin source files.  Some other file
  10.      needs this
  11.    **/
  12.  
  13. #include "ipaint.h"
  14. #include "istring.h"
  15. #include <stdio.h>
  16.  
  17. // IBrush creates the brush.  Calling IBrush with no arguments creates
  18. // the "none" brush.
  19.  
  20. IBrush::IBrush () : () 
  21. {
  22.     leftarrow = false;
  23.     rightarrow = false;
  24.     CalcDashPat(0xffff);
  25. }
  26.  
  27. IBrush::IBrush (int p, int w, boolean l, boolean r) : (p, w) 
  28. {
  29.     leftarrow = l;
  30.     rightarrow = r;
  31.     CalcDashPat(p);
  32. }
  33.  
  34. // Width overrides PBrush::Width when PBrush's value is the predefined
  35. // brush single because single cannot be trusted to return 1.  It
  36. // might return 0 because the X implementation might support only a
  37. // fast, device-dependent brush, not the standard single-width brush.
  38.  
  39. int IBrush::Width () 
  40. {
  41.     int width = PBrush::Width();
  42.  
  43.     if (value == single) 
  44.     {
  45.     width = 1;
  46.     }
  47.  
  48.     return width;
  49. }
  50.  
  51. // CalcDashPat calculates and stores the Postscript dash pattern
  52. // corresponding to the brush's line pattern.
  53.  
  54. void IBrush::CalcDashPat (int linepat) 
  55. {
  56.     linepat &= 0xffff;        // mask should always match patternWidth
  57.  
  58.     if (linepat == 0x0000)         // clear brush
  59.     {       
  60.     dashpatsize = 0;
  61.     dashoffset = -1;
  62.     }
  63.     else if (linepat == 0xffff)     // solid brush
  64.     { 
  65.     dashpatsize = 0;
  66.     dashoffset = 0;
  67.     }
  68.     else if (linepat == 0x5555)     // dotted brush, store 1 element not 16
  69.     { 
  70.     dashpat[0] = 1;
  71.     dashpatsize = 1;
  72.     dashoffset = 1;
  73.     }
  74.     else if (linepat == 0xaaaa)     // dotted brush, store 1 element not 16
  75.     { 
  76.     dashpat[0] = 1;
  77.     dashpatsize = 1;
  78.     dashoffset = 0;
  79.     }
  80.     else 
  81.     {
  82.     int i = 0;
  83.  
  84.     while (!((linepat << i) & 0x8000)) 
  85.     {
  86.         ++i;
  87.     }
  88.  
  89.     dashoffset = patternWidth - i + 1;
  90.  
  91.     int j = 0;
  92.     boolean currentrun = true;
  93.     int length = 0;
  94.  
  95.     for (int k = 0; k < patternWidth; k++) 
  96.     {
  97.         if ((((linepat << i) & 0x8000) != 0) == currentrun) 
  98.         {
  99.         ++length;
  100.         }
  101.         else 
  102.         {
  103.         dashpat[j++] = length;
  104.         currentrun = !currentrun;
  105.         length = 1;
  106.         }
  107.  
  108.         i = (i == patternWidth) ? 0 : i + 1;
  109.     }
  110.  
  111.     if (length > 0) 
  112.     {
  113.         dashpat[j] = length;
  114.     }
  115.  
  116.     dashpatsize = j + 1;
  117.     }
  118.  
  119.     const int Postscriptdashlimit = 11;
  120.  
  121.     if (dashpatsize > Postscriptdashlimit) 
  122.     {
  123.     fprintf(stderr, "Brush dash pattern 0x%x exceeds maximum ", linepat);
  124.     fprintf(stderr, "length of Postscript dash pattern with ");
  125.     fprintf(stderr, "%d elements, truncated to ", dashpatsize);
  126.     fprintf(stderr, "%d elements\n", Postscriptdashlimit);
  127.     dashpatsize = Postscriptdashlimit;
  128.     }
  129. }
  130.  
  131. // IColor creates the named color and stores its name.
  132.  
  133. IColor::IColor (const char* n) : (n) 
  134. {
  135.     name = strdup(n);
  136. }
  137.  
  138. // IColor creates the color using the given intensities and stores its
  139. // "name".
  140.  
  141. IColor::IColor (int r, int g, int b, const char* n) : (r, g, b) 
  142. {
  143.     name = strdup(n);
  144. }
  145.  
  146. // IColor stores the given color and its "name".
  147.  
  148. IColor::IColor (Color* color, const char* n) 
  149. {
  150.     value = color;
  151.     value->Reference();
  152.     name = strdup(n);
  153. }
  154.  
  155. // Delete frees storage allocated for the name.
  156.  
  157. IColor::~IColor () 
  158. {
  159.     delete name;
  160. }
  161.  
  162. // IFont creates the named font and stores the print font and size.
  163.  
  164. IFont::IFont (const char* name, const char* pf, const char* ps)
  165. : (FilterName(name)) 
  166. {
  167.     printfont = strdup(pf);
  168.     printsize = strdup(ps);
  169.     printfontandsize = new char[strlen(pf) + 1 + strlen(ps) + 1];
  170.     strcpy(printfontandsize, pf);
  171.     strcat(printfontandsize, " ");
  172.     strcat(printfontandsize, ps);
  173. }
  174.  
  175. // Delete frees storage allocated for the print font and size.
  176.  
  177. IFont::~IFont () 
  178. {
  179.     delete printfont;
  180.     delete printsize;
  181.     delete printfontandsize;
  182. }
  183.  
  184. // FilterName filters the name to ensure "stdfont" does not pass
  185. // through to PFont without being converted to nil.
  186.  
  187. const char* IFont::FilterName (const char* name) 
  188. {
  189.     if (strcmp(name, "stdfont") == 0) 
  190.     {
  191.     name = nil;
  192.     }
  193.  
  194.     return name;
  195. }
  196.  
  197. // IPattern creates the pattern.  Calling IPattern with no arguments
  198. // creates the "none" pattern, calling IPattern with an integer
  199. // creates a pattern from a 4x4 bitmap, calling IPattern with an array
  200. // and the actual size creates a pattern from a 16x16 bitmap that may
  201. // have originally been 8x8, and calling IPattern with an integer and
  202. // a float creates a grayscale pattern from a 4x4 bitmap.
  203.  
  204. IPattern::IPattern () : () 
  205. {
  206.     graylevel = -1;
  207.     size = 0;
  208. }
  209.  
  210. IPattern::IPattern (int dither, float g) : (dither) 
  211. {
  212.     graylevel = g;
  213.     size = 0;
  214. }
  215.  
  216. IPattern::IPattern (int data[patternHeight], int s) : (data) 
  217. {
  218.     graylevel = -1;
  219.     size = s;
  220. }
  221.  
  222. char* strdup (const char* s) 
  223. {
  224.     char* dup = new char[strlen(s) + 1];
  225.     strcpy(dup, s);
  226.     return dup;
  227. }
  228.